//
//  HMSelectPhoto.m
//  HMMarket
//
//  Created by haohao on 2017/6/27.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMSelectPhoto.h"
#import <AssetsLibrary/AssetsLibrary.h>

#define height 50
#define marin  10
#define btnHeight 10
#define totalNumber (_imageArray.count  + _dataArray.count)
@implementation HMSelectPhoto



- (void)setImageArray:(NSMutableArray *)imageArray {
    [self removeAllSubView];
    _imageArray = imageArray;
    self.frame = CGRectMake(KWidthSCREEN - height*totalNumber - marin * (totalNumber - 1) - 20, 5, height * totalNumber + marin * (totalNumber - 1), height);
    [self setUpSuView];
}

- (void)setDataArray:(NSMutableArray *)dataArray {
    [self removeAllSubView];
    _dataArray = dataArray;
    self.frame = CGRectMake(KWidthSCREEN - height*totalNumber - marin * (totalNumber - 1) - 20, 5, height * totalNumber + marin * (totalNumber - 1), height);
    [self setUpSuView];
}

- (void)setUpSuView {
    for (int i = 0; i < _imageArray.count; i++) {
    
        UIImageView *imageVI = [[UIImageView alloc] initWithFrame:CGRectMake(i * (height + marin), 0, height, height)];
        imageVI.userInteractionEnabled = YES;
        imageVI.image = _imageArray[i];
        [self addSubview:imageVI];
        
        UIButton *removeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        removeBtn.tag = i + 100;
        removeBtn.frame = CGRectMake(CGRectGetMaxY(imageVI.frame) - btnHeight, 0, btnHeight, btnHeight);
        [removeBtn setImage:[UIImage imageNamed:@"order_delete"] forState:UIControlStateNormal];
        [removeBtn addTarget:self action:@selector(removeImageVI:) forControlEvents:UIControlEventTouchUpInside];
        [imageVI addSubview:removeBtn];
    }
    
    for (int i = 0; i < _dataArray.count; i++) {
        UIImageView *imageVI = [[UIImageView alloc] initWithFrame:CGRectMake((i + _imageArray.count) * (height + marin), 0, height, height)];
        imageVI.userInteractionEnabled = YES;
        
        ALAsset *assetData = _dataArray[i];
        ALAssetRepresentation* representation = [assetData defaultRepresentation];
        imageVI.image = [UIImage imageWithCGImage:[representation fullResolutionImage]];
        [self addSubview:imageVI];
        
        UIButton *removeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        removeBtn.tag = 200 + i;
        removeBtn.frame = CGRectMake(CGRectGetMaxY(imageVI.frame) - btnHeight, 0, btnHeight, btnHeight);
        [removeBtn setImage:[UIImage imageNamed:@"order_delete"] forState:UIControlStateNormal];
        [removeBtn addTarget:self action:@selector(removeImageVI:) forControlEvents:UIControlEventTouchUpInside];
        [imageVI addSubview:removeBtn];
    }
}

- (void)removeImageVI:(UIButton *)sender{
    [self removeAllSubView];
    if (sender.tag >= 200) {
        [self.dataArray removeObjectAtIndex:sender.tag - 200];
        if ([_delegate respondsToSelector:@selector(returnImageArray:with:)]) {
            [_delegate returnImageArray:_dataArray with:DATA_ARRAY];
        }
    } else {
        [self.imageArray removeObjectAtIndex:sender.tag - 100];
        if ([_delegate respondsToSelector:@selector(returnImageArray:with:)]) {
            [_delegate returnImageArray:_imageArray with:IMAGE_ARRAY];
        }
    }
    self.frame = CGRectMake(KWidthSCREEN - height*totalNumber - marin * (totalNumber - 1) - 20, 5, height * totalNumber + marin * (totalNumber - 1), height);
    [self setUpSuView];
    
}

- (void)removeAllSubView {
    NSArray *array = self.subviews;
    for (int i = 0; i < array.count; i++) {
        UIView *subView = array[i];
        [subView removeFromSuperview];
    }
}

@end
